package zouhaotian.rbac.service.array;

import zouhaotian.rbac.domain.dto.DeptDto;
import zouhaotian.rbac.domain.dto.RoleDto;
import zouhaotian.rbac.domain.dto.UserDto;
import zouhaotian.rbac.domain.entity.Dept;
import zouhaotian.rbac.domain.entity.Role;
import zouhaotian.rbac.domain.entity.User;
import zouhaotian.rbac.domain.vo.Deptvo;
import zouhaotian.rbac.domain.vo.Result;
import zouhaotian.rbac.domain.vo.Rolevo;
import zouhaotian.rbac.domain.vo.Uservo;
import zouhaotian.rbac.service.IBaseService;
import zouhaotian.rbac.service.IDeptService;
import zouhaotian.rbac.service.IRoleService;
import zouhaotian.rbac.service.IUserService;
import zouhaotian.rbac.util.SingletonFactory;

import java.time.LocalDateTime;


/**
 * 业务层-用户
 *
 * @auther 邹昊天
 * @since 2025/6/20 16:31
 */

@Deprecated
public class UserService extends BaseService<User, UserDto, Uservo> implements IUserService{



    private final IDeptService deptService;
    private final IRoleService roleService;



    private UserService(){
        deptService = SingletonFactory.getInstance(DeptService.class);
        roleService = SingletonFactory.getInstance(RoleService.class);
        data = new User[DATA_SIZE + 1];
        temp = new Uservo[DATA_SIZE];
    }


    @Override
    Uservo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }

        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }

        return new Uservo(user.getId(), user.createAt, user.updateAt,
                user.username, user.name, user.mobile, user.email,
                user.deptId, deptName, user.roleId, roleName, user.enableed);
    }

    @Override
    User createEntity(UserDto dto, int id) {
        LocalDateTime now = LocalDateTime.now();
        return new User(id, now, now, dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }


    @Override
    void updateEntity(UserDto dto, User user) {
        user.updateAt = LocalDateTime.now();
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptId){
        boolean inUse = false;
        for (User user: data){
            if (user!=null && user.deptId==deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId){
        boolean inUse = false;
        for (User user: data){
            if (user!=null && user.roleId==roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }




    @Override
    public Result create(UserDto dto) {

        if (!deptService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.create(dto);
    }

    @Override
    public Result update(UserDto dto, int id) {

        if (!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
        return super.update(dto, id);
    }

    @Override
    public Result toggle(int id){
        if (!existsById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enableed = !data[id].enableed;
        return Result.success(data[id].enableed ? "启用成功" : "停用成功", null);
    }
}

